from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext

from configuration import fs_path

import os, subprocess

@login_required
def index(request):
  path = fs_path()
  g = os.path.join(os.path.dirname(__file__), 'damnfs.py')
  return render_to_response('damnfs.html', locals(), context_instance=RequestContext(request)) 
  
@login_required
def start(request):
  path = fs_path()
  g = os.path.join(os.path.dirname(__file__), 'damnfs.py')
  p = subprocess.Popen(['python', g, path], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  stdout, stderr = p.communicate()
  try:
    config = open(os.path.join(path, 'config'), 'rb').read()
  except IOError as e:
    config = str(e)
  return render_to_response('damnfs.html', locals(), context_instance=RequestContext(request)) 
 
@login_required
def stop(request):
  path = fs_path()
  g = os.path.join(os.path.dirname(__file__), 'damnfs.py')
  p = subprocess.Popen(['fusermount', '-u', path], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  stdout, stderr = p.communicate()
  return render_to_response('damnfs.html', locals(), context_instance=RequestContext(request)) 
